*Recoding ZA2820 

use "Data\ZA2820\ZA2820.dta" , clear

*Support for democracy
gen $idea_democracy				= v024	
*define missing values
mvdecode $idea_democracy, mv(9=.a)  //.a = no answer

*reverse scale
replace $idea_democracy = (6-idea_democracy)/5 if !missing($idea_democracy)	 

*Support for liberal democracy
gen $libdem_opposition			= v025g
gen $libdem_freeopinion			= v025e
gen $libdem_partyopportunity	= v025h
gen $libdem_criticism			= v025f						
gen $libdem_conflict			= v025a							
gen $libdem_generalwill			= v025j	

*Satisfaction with democracy
gen $satisfaction_democracy		= v008b		

*Vertrauen Institutionen
gen $trust_bundestag = v013b
gen $trust_government = v013c
gen $trust_verfassungsgericht = v013a
gen $trust_justice = v013h

*Efficacy
gen $politicians_takecare = v028b
gen $politicians_interestpublic = v028e
gen $ego_politicscomplicated = v028d


*define missing values
mvdecode $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
	$libdem_criticism $libdem_conflict $libdem_generalwill $satisfaction_democracy ///
	$politicians_takecare $politicians_interestpublic $ego_politicscomplicated ///
	$trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice, ///
	mv(9=.a)  //.a = no answer
	
mvdecode $politicians_takecare $politicians_interestpublic $ego_politicscomplicated, mv(7 8 = .a)

*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $libdem_opposition $libdem_freeopinion $libdem_partyopportunity {
	replace `var'	= (6-`var')/5 if !missing(`var')
}

*standardize remaining items 
replace $satisfaction_democracy = ($satisfaction_democracy-1)/4 if !missing($satisfaction_democracy)

foreach var of varlist $libdem_criticism $libdem_conflict $libdem_generalwill  {
	replace `var'	=(`var'-1)/5 if !missing(`var')
}

foreach var of varlist $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice  {
	replace `var' = (`var' - 1)/6 if !missing(`var')
}

foreach var of varlist $politicians_takecare $politicians_interestpublic {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}

foreach var of varlist $ego_politicscomplicated  {
	replace `var' = (5 - `var')/4 if !missing(`var')
}


*Sociodemographic Controls
gen		$gender 			= s001-1

gen 	$age				= 1995-s002b							  	//survey year - year of birth	

gen 	$education			= s003
replace $education 			= 0 if $education 	<= 7 | $education==9 	// Still in School, Berufsschüler, none, Hautpschulabschl., Mittlere Reife, Polyt. Obersch., Fachhochschul. = 0
replace $education 			= 1 if $education 	== 8 					// Abitur/EOS = 1

gen $eastwest 				= split-1

*Weight
*Sozialstrukturelles Ost-West Gewicht
*Bloßes Designgewicht: wicht
gen weight = kplwicht

*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(s022, 1, 2, 4)
replace $partyid_category	= 0 	if inlist(s022, 3, 5, 6)
replace $partyid_category	= -1 	if inlist(s022, 7, 8)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(s021, 1, 3)
replace $partyvote_category	= 0 	if inlist(s021, 2, 4, 5)
replace $partyvote_category	= -1 	if inlist(s021, 6, 7, 9, 10, 11)



*Meta variables
gen $dataset 					= "ZA2820"
gen $year 						= 1995

*check
tab $idea_democracy v024, m
tab $libdem_opposition v025g, m
tab $libdem_freeopinion v025e, m
tab $libdem_partyopportunity v025h, m
tab $libdem_criticism v025f, m						
tab $libdem_conflict v025a, m					
tab $libdem_generalwill v025j, m	
tab $satisfaction_democracy v008b, m	
tab $gender s001, m
tab $age s002b, m
tab $education s003, m
tab $eastwest split, m

*Save recode dataset
keep $idea_democracy $libdem_opposition	$libdem_freeopinion 					///
	$libdem_partyopportunity $libdem_criticism	$libdem_conflict 				///
	$politicians_takecare $politicians_interestpublic  $ego_politicscomplicated ///
	$trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice ///
	$libdem_generalwill $satisfaction_democracy $gender $age $education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
save ZA2820_recoded.dta, replace

